//
//  ChangePasswordViewController.m
//  phmall
//
//  Created by admin on 2017/11/9.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "ChangePasswordViewController.h"

@interface ChangePasswordViewController ()<UITextFieldDelegate>
{
    UITextField *name;
    UITextField *passWord;
    UITextField *confirm;
}

@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem=leftBar;
    
    name=[[UITextField alloc]init];
    name.delegate=self;
    name.secureTextEntry = YES;
    name.placeholder=Localized(@"原密码") ;
    if ([self.type isEqualToString:@"pay"]) {
        name.placeholder=Localized(@"原密码(未设置可为空)");
    }
   
    name.font=[UIFont systemFontOfSize:18];
    [self.view addSubview:name];
    [name mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(30);
        make.right.equalTo(self.view.mas_right).with.offset(-60);
        make.top.equalTo(self.view).with.offset(30);
        make.height.offset(40);
    }];
//    [name addTarget:self action:@selector(textField1TextChange:) forControlEvents:UIControlEventEditingChanged];
    
    
    
    UIView *nameLine=[[UILabel alloc]init];
    [self.view addSubview:nameLine];
    nameLine.backgroundColor=[UIColor orangeColor];
    [nameLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(name.mas_bottom);
        make.right.equalTo(self.view.mas_right).with.offset(-20);
        make.left.equalTo(self.view).with.offset(20);
        make.height.offset(1);
    }];
    
    passWord=[[UITextField alloc]init];
    passWord.placeholder=Localized(@"新密码");
    passWord.delegate=self;
    passWord.font=[UIFont systemFontOfSize:18];
    passWord.secureTextEntry = YES;
    [self.view addSubview:passWord];
    passWord.keyboardType=UIKeyboardTypeASCIICapable;
    [passWord mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(30);
        make.right.equalTo(self.view.mas_right).with.offset(-60);
        make.top.equalTo(name.mas_bottom).with.offset(30);
        make.height.offset(40);
    }];
 
    UIView *passLine=[[UILabel alloc]init];
    [self.view addSubview:passLine];
    passLine.backgroundColor=[UIColor orangeColor];
    [passLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passWord.mas_bottom);
        make.right.equalTo(self.view.mas_right).with.offset(-20);
        make.left.equalTo(self.view).with.offset(20);
        make.height.offset(1);
    }];
    
    confirm=[[UITextField alloc]init];
    confirm.placeholder=Localized(@"确认新密码");
    confirm.secureTextEntry = YES;
    confirm.delegate=self;
    confirm.keyboardType=UIKeyboardTypeASCIICapable;
    confirm.font=[UIFont systemFontOfSize:18];
    [self.view addSubview:confirm];
    [confirm mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(30);
        make.right.equalTo(self.view.mas_right).with.offset(-60);
        make.top.equalTo(passWord.mas_bottom).with.offset(30);
        make.height.offset(40);
    }];
    
 
    
    UIView *confirmLine=[[UILabel alloc]init];
    [self.view addSubview:confirmLine];
    confirmLine.backgroundColor=[UIColor orangeColor];
    [confirmLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(confirm.mas_bottom);
        make.right.equalTo(self.view.mas_right).with.offset(-20);
        make.left.equalTo(self.view).with.offset(20);
        make.height.offset(1);
    }];
    
    UIButton* next=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:next];
    [next setTitle:Localized(@"下一步") forState:UIControlStateNormal];
    [next addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    [next setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //    next.backgroundColor=[UIColor colorWithHexString:@"#ff9300"];
    //    ViewBorderRadius(next, 5, 1, [UIColor colorWithHexString:@"#ff9300"]);
    [next setBackgroundImage:[UIImage imageNamed:@"背景渐变条"] forState:UIControlStateNormal];
    [next mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(confirm.mas_bottom).with.offset(50);
        make.height.offset(44);
        make.left.equalTo(self.view).with.offset(20);
        make.right.equalTo(self.view.mas_right).with.offset(-20);
    }];
    // Do any additional setup after loading the view.
}
-(void)nextAction{
    if (passWord.text.length==0||name.text==nil) {
        NSLog(@"密码不可为空");
        [PublicClass ProgressHUDModeText:@"密码不可为空" andTime:2];
        //        [SVProgressHUD showErrorWithStatus:@"密码不可为空"];
        return;
    }
    if (confirm.text.length==0||name.text==nil) {
        NSLog(@"密码不可为空");
        [PublicClass ProgressHUDModeText:@"密码不可为空" andTime:2];
        //        [SVProgressHUD showErrorWithStatus:@"密码不可为空"];
        return;
    }
    if (passWord.text.length!=6) {
        NSLog(@"密码长度控制在6位");
        [PublicClass ProgressHUDModeText:@"密码长度控制为6位" andTime:2];
        //        [SVProgressHUD showErrorWithStatus:@"密码长度控制在6-18位"];
    }
 
    if (![passWord.text isEqualToString:confirm.text]) {
        NSLog(@"两次密码不同");
        [PublicClass ProgressHUDModeText:@"两次密码不同" andTime:2];
        //        [SVProgressHUD showErrorWithStatus:@"两次密码输入不同"];
        return;
    }

    NSString *urlstr =  [NSString stringWithFormat:@"%@%@",HomeURL,@"set_paypass"];
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
 
    [parametersDic setValue:name.text forKey:@"pass1"];
    [parametersDic setValue:passWord.text forKey:@"pass2"];
 
    [httpTool ZBPostNetDataWith:urlstr withDic:parametersDic andSuccess:^(id responseObject) {
        NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",responseDict);
        if ([responseDict[@"ret"] isEqualToString:@"200"]) {
            if ([responseDict[@"status"] isEqualToString:@"1"]){
                 [PublicClass ProgressHUDModeText:@"修改成功" andTime:2];
            }else{
                 [PublicClass ProgressHUDModeText:@"原密码错误" andTime:2];
            }
           
        }else{
            NSLog(@"%@",responseDict[@"err"]);
            [PublicClass ProgressHUDModeText:responseDict[@"err"] andTime:2];
        }
    } andFailure:^{
        [PublicClass ProgressHUDModeText:Localized(@"网络错误") andTime:2 ];
    }];
    
}
-(BOOL)judgePassWordLegal:(NSString *)pass{
    BOOL result = false;
    if ([pass length] >= 8){
        // 判断长度大于8位后再接着判断是否同时包含数字和字符
        NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:pass];
    }
    return !result;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
